package com.example.a2032.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.a2032.R
import com.example.a2032.activity.ChatActivity
import com.example.a2032.db.DataDbHelper
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMConversation
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMTextMessageBody
import com.hyphenate.easeui.constants.EaseConstant
import com.hyphenate.easeui.utils.EaseCommonUtils
import com.hyphenate.easeui.utils.EaseDateUtils
import com.hyphenate.easeui.utils.EaseSmileUtils
import java.util.*

class UnreadMsgAdapter(val listEMConversation: MutableList<EMConversation>, val knshipArray: Array<String>, val iconArrays: IntArray) : RecyclerView.Adapter<UnreadMsgAdapter.UnreadMsgViewHolder>() {

    class UnreadMsgViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivIcon = itemView.findViewById<ImageView>(R.id.ivUnreadMsgIcon)
        var tvName = itemView.findViewById<TextView>(R.id.textView_name)
        var tvContent = itemView.findViewById<TextView>(R.id.textView_content)
        var tvTime = itemView.findViewById<TextView>(R.id.textView_MsgTime)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UnreadMsgViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_unreadmsdg, parent, false);
        return UnreadMsgViewHolder(view);
    }

    override fun onBindViewHolder(holder: UnreadMsgViewHolder, position: Int) {
        var conversation = listEMConversation[position]
        var lastMsg = conversation.lastMessage
        val id = conversation.conversationId();
        var binders = DataDbHelper.getInstance().getBindersDao().findBinders(id)
        var name = when (conversation.isGroup) {
            true -> getGroupName(id)
            else -> knshipArray[binders.kinship]
        }
        val time = conversation.lastMessage.msgTime;
        val imageResId = when(conversation.isGroup){
            true -> R.drawable.icon_chat_group
            else -> iconArrays[binders.kinship]
        };
        holder.tvName.text = name;
        holder.tvTime.text = EaseDateUtils.getTimestampString(holder.itemView.context, Date(time));
        holder.tvContent.text = EaseSmileUtils.getSmiledText(holder.itemView.context, EaseCommonUtils.getMessageDigest(lastMsg, holder.itemView.context));
        holder.ivIcon.setImageResource(imageResId)
        holder.itemView.setOnClickListener {
            var type = 0;
            when (conversation.isGroup) {
                true -> EaseConstant.CHATTYPE_GROUP
                else -> EaseConstant.CHATTYPE_SINGLE
            }
            startActivity(holder.itemView.context, id, name, type)
           // conversation.markAllMessagesAsRead()
        }
    }



    fun getGroupName(id: String): String {
        val group = DataDbHelper.getInstance().getGroupsDao().findGroup(id)
        return group.name
    }

    fun startActivity(context: Context, id: String, name: String, type: Int) {
        ChatActivity.actionStart(context, id, name, type)

    }

    override fun getItemCount(): Int {
        return listEMConversation.size
    }

    var clickListener: OnItemClickListener? = null;
    fun setItemClickListener(clickListener: OnItemClickListener) {
        this.clickListener = clickListener;
    }

    public interface OnItemClickListener {
        fun onItemClick(position: Int) {
        };
    }
}